// Fill out your copyright notice in the Description page of Project Settings.


#include "MyPawn.h"

#include "Camera/CameraComponent.h"
#include "Engine/CollisionProfile.h"
#include "GameFramework/SpringArmComponent.h"
#include "Kismet/KismetMathLibrary.h"

// Sets default values
AMyPawn::AMyPawn()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	SetActorEnableCollision(false);
	/*CapsuleComponent = CreateDefaultSubobject<UCapsuleComponent>("CollisionCylinder");
	CapsuleComponent->InitCapsuleSize(34.0f, 88.0f);
	CapsuleComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);

	CapsuleComponent->CanCharacterStepUpOn = ECB_No;
	CapsuleComponent->SetShouldUpdatePhysicsVolume(true);
	CapsuleComponent->SetCanEverAffectNavigation(false);
	CapsuleComponent->bDynamicObstacle = true;
	RootComponent = CapsuleComponent;

	//相机组件
	CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
	CameraComp->SetupAttachment(CapsuleComponent, USpringArmComponent::SocketName);*/

}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void AMyPawn::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}

//左右旋转
void AMyPawn::FixedPointRotation_X(float Rate,AMyPlayerController *PlayerController)
{
	if(Rate != 0.0)
	{
		FVector ClickLocation = PlayerController->ClickLocation;
		FVector PawnLocation = GetActorLocation();
		FVector Afterlocation = UKismetMathLibrary::RotateAngleAxis(PawnLocation - ClickLocation,Rate,FVector(0,0,1));
		SetActorLocation(ClickLocation + Afterlocation);


		FRotator A = PlayerController->GetControlRotation();
		FRotator B = UKismetMathLibrary::RotatorFromAxisAndAngle(FVector(0,0,1),Rate);
		FRotator C = UKismetMathLibrary::ComposeRotators(A,B);
		PlayerController->SetControlRotation(C);
	}
}

//上下旋转
void AMyPawn::FixedPointRotation_Y(float Rate,AMyPlayerController *PlayerController)
{
	if(Rate != 0.0)
	{
		float HighMax = 100000000.0;
		float PitchMin = -85;
		float PotchMax = 85;

		
		FVector ClickLocation = PlayerController->ClickLocation;
		FVector PawnLocation = GetActorLocation();
		USceneComponent* PlayerRootComponent = PlayerController->GetRootComponent();
		FVector RightVector = (PlayerRootComponent != nullptr) ? PlayerRootComponent->GetRightVector() : FVector::RightVector;
		FVector Afterlocation = UKismetMathLibrary::RotateAngleAxis(PawnLocation - ClickLocation,Rate,RightVector);
		FVector EndLocation = ClickLocation + Afterlocation;
		
		
		FRotator A = PlayerController->GetControlRotation();
		FRotator B = UKismetMathLibrary::RotatorFromAxisAndAngle(RightVector,Rate);
		FRotator C = UKismetMathLibrary::ComposeRotators(A,B);
		
		bool b = EndLocation.Z <= HighMax;
		b &= C.Pitch >= PitchMin;
		b &= C.Pitch <= PotchMax;

		float Newpitch = FMath::Clamp(C.Pitch,PitchMin,PotchMax);
		FRotator NewRotator;
		NewRotator.Roll = 0.0;
		NewRotator.Pitch = Newpitch;
		NewRotator.Yaw = C.Yaw;

		if(b)
		{
			PlayerController->SetControlRotation(NewRotator);
			SetActorLocation(EndLocation);
		}
	}
}

void AMyPawn::MoveForward(float Rate)
{
	//Super::MoveForward(Rate);
	if (Rate != 0.f)
	{
		if (Controller)
		{
			FRotator const ControlSpaceRot = Controller->GetControlRotation();

			// transform to world space and add it
			AddMovementInput( FRotationMatrix(ControlSpaceRot).GetScaledAxis( EAxis::X ), Rate );
		}
	}
}

void AMyPawn::MoveRight(float Rate)
{
	//Super::MoveRight(Rate);
	if (Rate != 0.f)
	{
		if (Controller)
		{
			FRotator const ControlSpaceRot = Controller->GetControlRotation();

			// transform to world space and add it
			AddMovementInput( FRotationMatrix(ControlSpaceRot).GetScaledAxis( EAxis::Y ), Rate );
		}
	}
}

void AMyPawn::MoveUp_World(float Rate)
{
	if (Rate != 0.f)
	{
		AddMovementInput(FVector::UpVector, Rate);
	}
}

/*
void AMyPawn::MoveForward(float AxisValue)
{
	if (AxisValue != 0.f)
	{
		if (Controller)
		{
			FRotator const ControlSpaceRot = Controller->GetControlRotation();

			// transform to world space and add it
			AddMovementInput( FRotationMatrix(ControlSpaceRot).GetScaledAxis( EAxis::X ), AxisValue );
		}
	}
}

void AMyPawn::MoveRight(float AxisValue)
{
	if (AxisValue != 0.f)
	{
		if (Controller)
		{
			FRotator const ControlSpaceRot = Controller->GetControlRotation();

			// transform to world space and add it
			AddMovementInput( FRotationMatrix(ControlSpaceRot).GetScaledAxis( EAxis::Y ), AxisValue );
		}
	}
}

void AMyPawn::MoveUp(float AxisValue)
{
	if (AxisValue != 0.f)
	{
		AddMovementInput(FVector::UpVector, AxisValue);
	}
}*/

